from django.contrib import admin
from django.contrib.admin import ModelAdmin, TabularInline
from django.utils.html import format_html
from django.contrib.admin import action
from django.contrib.admin.filters import DateFieldListFilter
from auditlog.registry import auditlog
from django_otp.admin import OTPAdminSite
from django_mptt_admin.admin import DjangoMpttAdmin
from django_jsonform.models.fields import JSONField
from django_jsonform.widgets import JSONFormWidget
from django.http import HttpResponse
from django.utils import timezone
from django.db.models.fields import DurationField as TimedeltaField
from import_export.admin import ImportExportMixin
from import_export import resources

from django.contrib.auth import get_user_model
from .models import UserProfile, Role, ProductCategory, ProductSPU, ProductSKU, StockLog, Payment, Order, Coupon, SystemLog, ReturnOrder, DiscountCampaign, MessageTemplate, PushRecord

# User = get_user_model()

# @admin.register(UserProfile)
# class UserProfileAdmin(admin.ModelAdmin):
#     list_display = ('username', 'email', 'phone', 'is_verified', 'is_staff')
#     list_filter = ('is_verified', 'is_staff', 'is_superuser')
#     search_fields = ('username', 'email', 'phone')
#     filter_horizontal = ('groups', 'user_permissions')

# # 启用两步验证
# admin.site.__class__ = OTPAdminSite

# 商品运营管理
@admin.register(ProductCategory)
class ProductCategoryAdmin(ModelAdmin):
    list_display = ('name', 'parent', 'level', 'description')
    list_filter = ('level',)
    search_fields = ('name', 'description')
    list_select_related = ('parent',)

class ProductSKUInline(TabularInline):
    model = ProductSKU
    extra = 1
    formfield_overrides = {
        JSONField: {'widget': JSONFormWidget(schema={
            'type': 'object',
            'properties': {
                'specifications': {'type': 'object'}
            }
        }, attrs={'class': 'jsoneditor'})}
    }

@admin.register(ProductSPU)
class ProductAdmin(ImportExportMixin, ModelAdmin):
    list_display = ('name', 'category', 'price', 'stock', 'status')
    list_filter = ('category', 'status')
    search_fields = ('name', 'sku')
    raw_id_fields = ('category',)
    list_select_related = ('category',)
    inlines = [ProductSKUInline]
    formfield_overrides = {
        JSONField: {'widget': JSONFormWidget(schema={
            'type': 'object',
            'properties': {
                'attributes': {'type': 'object'},
                'specifications': {'type': 'object'}
            }
        }, attrs={'class': 'jsoneditor'})}
    }

@admin.register(ProductSKU)
class ProductSKUAdmin(ModelAdmin):
    list_display = ('spu', 'price', 'original_price', 'created_at')
    list_filter = ('spu',)
    search_fields = ('spu__name', 'spec_hash')
    list_select_related = ('spu',)

@admin.register(StockLog)
class StockLogAdmin(ModelAdmin):
    list_display = ('sku', 'operation', 'quantity', 'before_quantity', 'after_quantity', 'created_at')
    list_filter = ('operation',)
    search_fields = ('sku__spu__name', 'reference_id', 'operator')
    list_select_related = ('sku', 'sku__spu')
    date_hierarchy = 'created_at'

@admin.register(Payment)
class PaymentAdmin(ModelAdmin):
    list_display = ('order', 'payment_method', 'amount', 'status', 'created_at')
    list_filter = ('payment_method', 'status')
    search_fields = ('order__order_no', 'payment_no', 'third_party_no')
    list_select_related = ('order',)
    date_hierarchy = 'created_at'